package com.xhu.collegestudentrentalsystem.module.user.controller;

import com.xhu.collegestudentrentalsystem.annocation.LoginRequired;
import com.xhu.collegestudentrentalsystem.module.user.entity.House;
import com.xhu.collegestudentrentalsystem.module.user.entity.Page;
import com.xhu.collegestudentrentalsystem.module.user.entity.UserMark;
import com.xhu.collegestudentrentalsystem.module.user.service.impl.MarkServiceImpl;
import com.xhu.collegestudentrentalsystem.util.HostHolder;
import com.xhu.collegestudentrentalsystem.util.ResultData;
import com.xhu.collegestudentrentalsystem.util.constant.KeyMsg;
import com.xhu.collegestudentrentalsystem.util.constant.PointMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/12 15:22
 */
@Controller
@RequestMapping("/mark")
public class MarkController {

    @Autowired
    private MarkServiceImpl markService;

    @Autowired
    private HostHolder hostHolder;

    /**
     * 用户在收藏详情页面，收藏房子信息
     *      如果已收藏，则删除信息，即取消收藏
     */
    @RequestMapping("/userMark")
    @ResponseBody
    public ResultData userMark(Long houseId){
        UserMark userMark = new UserMark();
        userMark.setCreateTime(new Date());
        userMark.setHouseId(houseId);

        // 没有登录时
        if(hostHolder.getUser() == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        userMark.setUserId(hostHolder.getUser().getId());

        // 查询是否已收藏，已收藏则取消收藏
        UserMark markEntity = markService.findUserMarkById(houseId, hostHolder.getUser().getId());
        if(markEntity == null){
            Integer mark = markService.markHouse(userMark);
            if(mark > 0){
                return new ResultData(ResultData.MARK_SUCCESS, PointMsg.mark.MARK_SUCCESS);
            }else {
                return new ResultData(ResultData.OPERATION_FAIL, PointMsg.mark.MARK_FAIL);
            }
        }else{
            Integer cancel = markService.cancelMarkHouse(userMark);
            if(cancel > 0){
                return new ResultData(ResultData.CANCEL_MARK_SUCCESS, PointMsg.mark.CANCEL_MARK_SUCCESS);
            }else {
                return new ResultData(ResultData.OPERATION_FAIL, PointMsg.mark.CANCEL_MARK_FAIL);
            }
        }
    }

    /**
     * 查询用户的收藏列表
     */
    @LoginRequired
    @RequestMapping("/markList")
    public String markList(Model model, Page page){
        page.setLimit(3);
        List<House> markList = markService.findUserMarkList(hostHolder.getUser().getId(), page);
        model.addAttribute(KeyMsg.house.HOUSE_MARK, markList);
        page.setPath("/mark/markList");
        return "site/mark-list";
    }

    /**
     * 用户取消收藏
     */
    @LoginRequired
    @RequestMapping("/cancelMark")
    public String cancelMark(Long houseId, Page page, Model model){
        UserMark userMark = new UserMark();
        userMark.setUserId(hostHolder.getUser().getId());
        userMark.setHouseId(houseId);
        Integer cancel = markService.cancelMarkHouse(userMark);
        if(cancel > 0){
            model.addAttribute(KeyMsg.MSG, PointMsg.mark.CANCEL_MARK_SUCCESS);
            // 重新查询信息封装
            markList(model, page);
        }else {
            model.addAttribute(KeyMsg.MSG, PointMsg.mark.CANCEL_MARK_FAIL);
        }
        return "site/mark-list";
    }


}
